package cn.jishiyu11.xeqb.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import cn.jishiyu11.xeqb.EntityClass.FragmengData.CardData
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.utils.ImageLoadUtils

/**
 * Created by jsy_zj on 2018-03-06.
 * 卡片管理-----卡片列表fragmentRecyclerView  Adapter
 */
class CardRecyclerAdapter(context: Context,list: MutableList<CardData>):RecyclerView.Adapter<CardRecyclerAdapter.ViewHolder>(){

    var context: Context? = null
    var list:MutableList<CardData>? = null
    var inflater: LayoutInflater? = null

    init {
        this.context = context
        this.list = list
        this.inflater = LayoutInflater.from(this.context)
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        var cardData = list?.get(position)
        holder?.title?.text = cardData?.title
        holder?.describ?.text = cardData?.describ
        holder?.addTime?.text = cardData?.addTime

//        Glide.with(context).load(cardData?.imgUrl)
//                .asBitmap()
//                .into(holder?.imgUrl)
        cardData?.imgUrl?.let { ImageLoadUtils.display(context!!,holder?.imgUrl,it) }

        holder?.delete?.setOnClickListener {
            list?.removeAt(position)
            this.notifyDataSetChanged()
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        return ViewHolder(inflater?.inflate(R.layout.frg_cardmanager_recycler_item, parent, false), context!!)
    }

    class ViewHolder(itemView: View?, context: Context):RecyclerView.ViewHolder(itemView) {
        var title:TextView = itemView?.findViewById(R.id.frg_cardmanager_recycler_item_title)!!
        var imgUrl:ImageView = itemView?.findViewById(R.id.frg_cardmanager_recycler_item_img)!!
        var describ:TextView = itemView?.findViewById(R.id.frg_cardmanager_recycler_item_discrib)!!
        var addTime:TextView = itemView?.findViewById(R.id.frg_cardmanager_recycler_item_addtime)!!
        var delete:TextView = itemView?.findViewById(R.id.frg_cardmanager_recycler_item_delete)!!
    }
}